package com.itheima.organization.controller;


import com.github.pagehelper.Page;
import com.itheima.aom.pojo.Aom;
import com.itheima.aom.pojo.AomRole;
import com.itheima.aom.pojo.QueryPageBean;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.organization.service.AomRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/aomRole")
public class AomRoleController {

    @Autowired
    private AomRoleService aomRoleService;

    /**
     * 新增角色
     * @param aomRole
     * @return
     */
    @PostMapping("/add")
    public Result addRole(@RequestBody AomRole aomRole){
        try {
            aomRoleService.addRole(aomRole);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "新增角色失败");
        }
        return new Result(true, StatusCode.OK, "新增角色成功");
    }

    /**
     * 修改角色
     * @param aomRole
     * @return
     */
    @PutMapping("/update")
    public Result updateRole(@RequestBody AomRole aomRole){
        try {
            aomRoleService.updateRole(aomRole);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, StatusCode.ERROR, "修改角色失败");
        }
        return new Result(true, StatusCode.OK, "修改角色成功");
    }

    /**
     * 删除角色
     * @param aomRole
     * @return
     */
    @DeleteMapping("/delete")
    public Result deleteRole(@RequestBody AomRole aomRole){
        try {
            aomRoleService.delete(aomRole);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, StatusCode.ERROR, "删除角色失败");
        }
        return new Result(true, StatusCode.OK, "删除角色成功");
    }

    /**
     * 分页展示角色
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/findPage/{page}/{size}")
    public Result findPage(@RequestParam Map<String, Object> searchMap, @PathVariable Integer page, @PathVariable Integer size){

        try {
            Page<AomRole> aomRolePage = aomRoleService.findPage(searchMap, page, size);
            PageResult<AomRole> aomRolePageResult = new PageResult<>(aomRolePage.getTotal(), aomRolePage.getResult());
            return new Result(true, StatusCode.OK, "分页查询角色成功", aomRolePageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, StatusCode.ERROR, "分页查询角色失败");
        }

    }
}
